package com.lanswon.qzsmk.controller.systemManageController;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.controller.cardManageController.BaseController;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.ErrorCode;
import com.lanswon.qzsmk.model.Role;
import com.lanswon.qzsmk.model.RolePrivilege;
import com.lanswon.qzsmk.service.systemManageService.PrivilegeManageService;
import com.lanswon.qzsmk.service.systemManageService.RoleManageService;
import com.lanswon.qzsmk.vo.MenuVo;
import com.lanswon.qzsmk.vo.QueryRoleVo;
import com.lanswon.qzsmk.vo.UserRoleVo;



/**
 * 角色管理Controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="/roleManage")
public class RoleManageController  {
	
	@Autowired
	RoleManageService rms;
	
	@Autowired
	PrivilegeManageService pms;

	/**
	 * 链接角色管理
	 * @return
	 */
	@RequiresPermissions("roleManage:index")
	@RequestMapping(value="/roleManageIndex",name="角色管理,roleManage:index",method={RequestMethod.GET,RequestMethod.POST})
	public String roleManageIndex(){
		
		return "RoleManage";
	}

	/**
	 * 链接新增角色
	 * @return
	 */
	@RequiresPermissions("roleManage:addRole")
	@RequestMapping(value="/addRole",name="角色管理新增角色,roleManage:addRole",method=RequestMethod.GET)
	public String addRole(){
		
		return "AddRole";
	}
	
	/**
	 * 链接功能授权
	 * @return
	 */
	@RequiresPermissions("roleManage:functionAccredit")
	@RequestMapping(value="/functionAccredit",name="角色管理功能授权,roleManage:functionAccredit",method=RequestMethod.GET)
	public String functionAccredit(String parm,HttpServletRequest request){
		System.out.println(parm+"<<======角色ID");
		Role role=rms.queryRoleById(Integer.valueOf(parm));
		request.getSession().setAttribute("role", role);
		return "FunctionAccredit";
	}
	
	/**
	 * 获取角色信息
	 * @param qrv
	 * @param pageCount
	 * @return
	 */
	@RequiresPermissions("roleManage:getInfoByRole")
	@ResponseBody
	@RequestMapping(value="/getInfoByRole",name="角色管理获取角色信息,roleManage:getInfoByRole",method=RequestMethod.POST)
	public  DwzPage<Role> getInfoByRole(QueryRoleVo qrv,Integer numPerPage,Integer pageNum){
//		if(qrv.getNumPerPage()==null){//加载页面时执行
//			qrv.setCurrentPage(1);
//			qrv.setNumPerPage(20);
//		}else {
//			if(qrv.getCurrentPage()<=1||pageCount==0){
//				qrv.setCurrentPage(1);
//			}else if(qrv.getCurrentPage()>=pageCount&&pageCount!=0){
//				qrv.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		qrv.setCurrentPage(pageNum);
		qrv.setNumPerPage(numPerPage);
		qrv.setStartLine((qrv.getCurrentPage()-1)*qrv.getNumPerPage());
		DwzPage<Role> dPage=rms.queryRole(qrv);
		return dPage;
	}
	
	/**
	 * 增加角色
	 * @param role
	 * @return
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("roleManage:insertRole")
	@ResponseBody
	@RequestMapping(value="/insertRole",name="角色管理增加角色,roleManage:insertRole",method=RequestMethod.POST)
	public Map<String,Object> insertRole(Role role) throws ApplicationException{
		Map<String, Object> map=new HashMap<>();
		rms.addRole(role);
		map.put("statusCode", "200");
		map.put("message", "操作成功");
		map.put("navTabId", "roleManage");
		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "roleManage/roleManageIndex");//post请求
		return map;
		
	}
	
	/**
	 * 删除角色
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("roleManage:deleteRole")
	@ResponseBody
	@RequestMapping(value="/deleteRole",name="角色管理删除角色,roleManage:deleteRole",method=RequestMethod.POST)
	public Map<String, Object>deleteRole(String[] roles) throws Exception{
		Map<String, Object>map=new HashMap<>();
		List<Integer> roleIds=new ArrayList<>();
		for(String str:roles){
			roleIds.add(Integer.valueOf(str));
		}
		rms.deleteRoles(roleIds);
		map.put("statusCode", "200");
		map.put("message", "删除任务成功");
		map.put("navTabId", "roleManage");
		map.put("forwardUrl", "roleManage/roleManageIndex");//post请求
		return map;
	}
	
	/**
	 * 链接人员设置
	 * @param parm
	 * @return
	 */
	@RequiresPermissions("roleManage:goPersonnelSet")
	@RequestMapping(value="/goPersonnelSet",name="角色管理链接人员设置,roleManage:goPersonnelSet",method=RequestMethod.GET)
	public String personnelSet(String parm,HttpServletRequest request) {
		request.getSession().setAttribute("parm",parm);
		System.out.println(parm+"===================>>>>");
		Role role=rms.queryRoleById(Integer.valueOf(parm));
		request.getSession().setAttribute("role", role);
		return "PersonalSet";
	}
	
	/**
	 * 人员设置和功能授权显示角色信息
	 * @param request
	 * @return
	 */
	@RequiresPermissions("roleManage:getRoleByPersonalSet")
	@ResponseBody
	@RequestMapping(value="/getRoleByPersonalSet",name="角色管理人员设置和功能授权显示角色信息,roleManage:getRoleByPersonalSet",method=RequestMethod.POST)
	public Role getRoleByPersonalSet(HttpServletRequest request){
		HttpSession session=request.getSession();
		Role role=(Role) session.getAttribute("role");
		return role;
	}
	
	/**
	 * 人员设置时获取柜员信息
	 * @param request
	 * @throws Exception 
	 */
	@RequiresPermissions("roleManage:getTellerInfoByPersonalSet")
	@RequestMapping(value="/getTellerInfoByPersonalSet",name="角色管理人员设置获取信息,roleManage:getTellerInfoByPersonalSet",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<UserRoleVo> getTellerInfoByPersonalSet(HttpServletRequest request,UserRoleVo urv,Integer pageNum,Integer numPerPage) throws Exception{
		String roleId=(String) request.getSession().getAttribute("parm");
		request.getSession().removeAttribute("parm");
		if(roleId!=null){
			urv.setRoleId(Integer.valueOf(roleId));
		}
//		if(urv.getNumPerPage()==null){//加载页面时执行
//			urv.setCurrentPage(1);
//			urv.setNumPerPage(20);
//		}else {
//			if(urv.getCurrentPage()<=1||pageCount==0){
//				urv.setCurrentPage(1);
//			}else if(urv.getCurrentPage()>=pageCount&&pageCount!=0){
//				urv.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		urv.setCurrentPage(pageNum);
		urv.setNumPerPage(numPerPage);
		urv.setStartLine((urv.getCurrentPage()-1)*urv.getNumPerPage());
		DwzPage<UserRoleVo> dPage=rms.showUsers(urv);
		return dPage;
	}
	
	/**
	 * 提交人员设置
	 * @param sysId
	 * @param roleId
	 * @param userNo
	 * @return
	 */
	@RequiresPermissions("roleManage:commitPersonalSet")
	@ResponseBody
	@RequestMapping(value="/commitPersonalSet",name="角色管理提交人员设置,roleManage:commitPersonalSet",method=RequestMethod.POST)
	public Map<String, Object> commitPersonalSet(String sysId,String roleId,String userNo,String sign,String [] options){
		Map<String, Object> map=new HashMap<>();
		if(options!=null){
			for(String str:options){
				System.out.println(str+"<<<========页面元素===========>>>");
				if(!str.equals("0,0")){//去掉柜员授权页面添加的元素
					String [] array=str.split(",");
					for(int i=1;i<array.length;i++){
						rms.addUser(Integer.valueOf(userNo), Integer.valueOf(array[1]), Integer.valueOf(array[0]), sign);
					}
				}	
			}		
		}else{
			System.out.println(sysId+","+roleId+","+userNo+","+sign);
			rms.addUser(Integer.valueOf(userNo), Integer.valueOf(sysId), Integer.valueOf(roleId), sign);
		}
		
		map.put("statusCode", "200");
		return map;
	}
	

	/**
	 * 获取所有权限
	 * @return
	 */
	@RequiresPermissions("roleManage:allPermission")
	@ResponseBody
	@RequestMapping(value="allPermission",name="角色管理获取所有权限及角色已有权限,roleManage:allPermission",method=RequestMethod.POST)
	public Map<String,Object> allPermission(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		List<MenuVo> meaus=pms.querySysPermissionMenus();
		HttpSession session=request.getSession();
		Role role=(Role) session.getAttribute("role");
		session.removeAttribute("role");
		List<Integer> mVos=pms.querySysPermissionByRoleId_1(Integer.valueOf(role.getRoleId()));
		map.put("meaus", meaus);
		map.put("mVos", mVos);
		return map;
	}
	
	
	/**
	 * 为角色设置权限
	 * @param sysId
	 * @param roleId
	 * @param permissions
	 * @param sign
	 * @return
	 */
	@RequiresPermissions("roleManage:setPermission")
	@ResponseBody
	@RequestMapping(value="/setPermission",name="角色管理为角色设置权限,roleManage:setPermission",method=RequestMethod.POST)
	public Map<String, Object> setPermission(String sysId,String roleId,String[]permissions,boolean sign){
		for(String str:permissions){
			System.out.println(sysId+","+roleId+","+str+","+sign);
			RolePrivilege r=new RolePrivilege(sysId,roleId,str);
			if(sign){
				pms.setAuthForRole(r);
			}else{
				pms.cancelAuthForRole(r);
			}
		}
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", "200");
		return map;
	}
}
